package com.sf.dlb.service;

import com.sf.dlb.config.Global;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.jpa.model.*;
import com.sf.dlb.jpa.repository.*;
import com.sf.dlb.mapper.dao.UserInvitationExtractMapper;
import com.sf.dlb.mapper.entity.InvitationRecordEntity;
import com.sf.dlb.mapper.entity.InvitationUserEntity;
import com.sf.dlb.mapper.entity.UserGetDlbNumberEntity;
import com.sf.dlb.mapper.entity.UserInvitationExtractEntity;
import com.sf.dlb.utils.*;
import com.sf.dlb.vo.InvitationRecordVO;
import com.sf.dlb.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by jing.tan on 2018/5/21.
 */
@Service
public class UserInvitationService {
    private static Logger logger = LoggerFactory.getLogger(UserInvitationService.class);
    @Autowired
    private UserInvitationExtractMapper userInvitationExtractMapper;
    @Autowired
    private UserInfoRepository userInfoRepository;
    @Autowired
    private UserDlbRecordRepository userDlbRecordRepository;
    @Resource
    private Global global;

    /**
     * 查询用户邀请记录
     * @param userId
     * @return
     */
    public ResultVO invitationList(String userId){
        logger.info("invitationList start");
        List<InvitationRecordEntity> invitationRecordEntities=userInvitationExtractMapper.findByInvitationRecord(userId,"0");
        int realCount=0;
        int gradeCount=0;
        //查询此用户购买奖励流水
        List<UserDlbRecordInfo> userDlbRecordInfos=userDlbRecordRepository.findByUserIdAndSourceAndDelFlag(userId,ResponseConstant.source_type_buy_grade_divide,"0");
        for (InvitationRecordEntity invitationRecordEntity:invitationRecordEntities) {
            String photo= StringUtils.isEmpty(invitationRecordEntity.getPhoto())?"":global.file_public_ip +invitationRecordEntity.getPhoto();
            invitationRecordEntity.setPhoto(photo);
            String realValue=StateToValueUtils.switchRealStateValue(invitationRecordEntity.getReal());
            invitationRecordEntity.setReal(realValue);
            if(invitationRecordEntity.getReal().equals("已实名")){
                realCount++;
            }
            invitationRecordEntity.setGradeValue(StateToValueUtils.switchGradeValue(invitationRecordEntity.getGrade()));
            //获得此用户多少奖励
            String useUserId=invitationRecordEntity.getUseUserId();
            double buyGradeGetDlbCount=0;
            for (UserDlbRecordInfo userDlbRecordInfo:userDlbRecordInfos) {
                String sourceDesc=userDlbRecordInfo.getSourceDesc();
                Double dlbNumber=userDlbRecordInfo.getDlbNumber();
                String[] sourceDescSplit=sourceDesc.split(":");
                String buyGradeUserId=sourceDescSplit[2];
                if(StringUtils.equals(buyGradeUserId,useUserId)){
                    buyGradeGetDlbCount=Arith.add(buyGradeGetDlbCount,dlbNumber);
                }
            }
            //是会员的，有多少人
            if(!StringUtils.equals(invitationRecordEntity.getGrade(),"0")){
                gradeCount++;
            }
            invitationRecordEntity.setBuyGradeGetDlb(((int)buyGradeGetDlbCount)+"");
        }
        InvitationRecordVO invitationRecordVO=new InvitationRecordVO();
        invitationRecordVO.setRealCount(realCount);
        invitationRecordVO.setGradeCount(gradeCount);
        invitationRecordVO.setInvitationCount(invitationRecordEntities.size());
        invitationRecordVO.setInvitationRecordEntityList(invitationRecordEntities);
        return ResultVOUtil.success(invitationRecordVO);
    }

    /**
     * 生成邀请码-单个
     */
    public String generateInviteCode(){
        String verificationCode= IdentifyCodeUtil.createVerificationCode();
        UserInfo userInfo=userInfoRepository.findByQrCodeAndDelFlag(verificationCode,"0");
        if(null!=userInfo){//如果生成的邀请码重复了，那么重新生成，直到生成唯一的邀请码
            return generateInviteCode();
        }else{
            return verificationCode;
        }
    }
}
